<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MContests
 *
 * @author andrea
 */
class MScores_vidwiz extends Model_vidwiz{
    
    public function __construct( ) {
        global $config;
        $this->tableName = $config['tables']['scores'];
        $this->installationFields = array(
                  array(
                      0 => 'id',
                      1 => 'int',
                      2 => 'NOT NULL AUTO_INCREMENT PRIMARY KEY'
                  ),
                  array(
                      0 => 'userid',
                      1 => 'INT',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => 'videoid',
                      1 => 'INT',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => 'score',
                      1 => 'DOUBLE',
                      2 => 'NOT NULL'
                  ),
                  array(
                      0 => 'contestid',
                      1 => 'INT',
                      2 => 'NOT NULL'
                  )
                );
    }
    
    public function submitScore($score,$videoid,$userid,$contestid){
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        $entity = USingleton_vidwiz::getInstances("EScores_vidwiz");

        $fields = array(
            "userid"    => $userid,
            "videoid"   => $videoid,
            "score"     => $score,
            "contestid" => $contestid
        );
        $object = $entity->generateObject($fields);
        $query = $this->insertQuery($object);
        
        $this->executeQuery($query);
    }
    
    
    public function getCharts($contestid){
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        $MContests = new MContests_vidwiz();
        global $config;
        $contest = $MContests->getContestFromId($contestid);
        $return = false;
        if($contest['winnerSelection'] == "0"){
            $return = $this->getChartsType0($contestid);
            }
        else{
            $return = $this->getChartsType1($contestid);
        }
        return $return;
    }
    
    public function getChartsType1($contestid){
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        $MContests = new MContests_vidwiz();
        global $config;
        $where = Array(
            array("contestid","=",$contestid)
        );
        $select = array(
            "videoid",
            "COUNT(videoid) as scoreNr",
            "AVG(score) as score",
            "publisher_username"
        );
        $order = "score ASC, scoreNr ASC, videoid DESC";
        $options = "GROUP BY videoid";
        $join = array(
            array($config['tables']['scores'].".videoid",$config['tables']['videos'].".id")
        );
        $rated = $this->searchQuery(array($config['tables']['scores'],$config['tables']['videos']), $where, $select, false, $order, $join, $options);
        
        $ratedId = $this->searchQuery(array($config['tables']['scores'],$config['tables']['videos']), $where, array($config['tables']['scores'].".videoid"), false, false, $join, $options);
        $where = Array(
            array("contestid","=",$contestid),
            array("videoid"," NOT IN ","(".$ratedId.")")
        );
        $join = array(
            array($config['tables']['partecipations'].".videoid",$config['tables']['videos'].".id")
        );
        $notRated = $this->searchQuery(array($config['tables']['partecipations'],$config['tables']['videos']), $where, array("videoid","publisher_username"), false, "videoid DESC", $join, "GROUP BY videoid");
        
        $results['rated'] = $this->getRowsAssoc($rated);
        $results['notRated'] = $this->getRowsAssoc($notRated);
        foreach($results['notRated'] as $key => $value){
            $results['notRated'][$key]["scoreNr"] = "0";
            $results['notRated'][$key]["score"] = "0";
        }
        $results = array_merge($results['notRated'],$results['rated']);
        
        return $results;
    }
    
    public function getChartsType0($contestid){
        $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
        $MContests = new MContests_vidwiz();
        global $config;
        $where = Array(
            array("contestid","=",$contestid),
            array("placing","<>","0")
        );
        $select = array(
            "videoid",
            "publisher_username",
            "placing"
        );
        $order = "placing DESC";
        $options = "GROUP BY videoid";
        $join = array(
            array($config['tables']['partecipations'].".videoid",$config['tables']['videos'].".id")
        );
        $rated = $this->searchQuery(array($config['tables']['partecipations'],$config['tables']['videos']), $where, $select, false, $order,$join,$options);
        $results = $this->getRowsAssoc($rated);
        return $results;
    
    }
    
    
}

?>
